package com.jiaai.web.zfp_back.member;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.gift.api.IGiftHisApi;
import com.jiaai.member.api.IMemberApi;
import com.jiaai.member.model.BMember;
import com.jiaai.member.model.MemberQueryVO;
import com.jiaai.util.RestResultGenerator;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * class_name:ZBMemberController
 * describe:completed
 * create_user:张昉平
 * create_date:2017/12/18
 * create_date:14:39
 */
@RestController
@RequestMapping("/api/v1/admin/member")
public class ZBMemberController {

    @Reference
    private IMemberApi iMemberApi;

   @Reference
   private IGiftHisApi iGiftHisApi;
    @GetMapping("/oneInfo")
    public Object oneInfo(@RequestParam String mId){
//        Map map = iMemberApi.oneInfo(mId);
//        map.put("sendGift", iGiftHisApi.getOneAllHis(mId, 1));//送出去的礼物
//        if (!map.get("mId").toString().equals("null"))map.put("getGift", iGiftHisApi.getOneAllHis(map.get("mId").toString(), 2));//收到的礼物
//        else map.put("getGift", new ArrayList<>());//收到的礼物
        return RestResultGenerator.genSuccessResult(null);
    }

    @GetMapping("/list")
    public Object list(@RequestParam Integer curr, MemberQueryVO vo){
        return RestResultGenerator.genSuccessResult(iMemberApi.backMemberList(curr, vo));
    }

    @GetMapping("/getInfo")
    public Object getInfo(@RequestParam String mId) throws Exception{
        return RestResultGenerator.genSuccessResult(iMemberApi.findMemberInfoById(mId));
    }

    @PostMapping("/changestatus")
    public Object changeStatus(@RequestParam String mId, @RequestParam Integer STATUS){
        iMemberApi.changeStatus(mId, STATUS);
        return RestResultGenerator.genSuccessResult("成功");
    }

    @PostMapping("/changeInfor")
    public Object changeInfor(@RequestBody BMember bMember) throws Exception{
        iMemberApi.changeInfo(bMember);
        return RestResultGenerator.genSuccessResult("成功");
    }
    @PostMapping("/editMember")
    public Object editMember(@RequestBody BMember bMember) throws Exception{
        iMemberApi.editMember(bMember);
        return RestResultGenerator.genSuccessResult("成功");
    }

    @GetMapping("/newnum")
    public Object tongJiNewMember(){
        TreeMap map = (TreeMap) iMemberApi.tongJiNewMember();
        List<Map<String, String>> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        List temp = new ArrayList(map.values());

        for (int i = 0; i < temp.size(); i++) {
            String date = sdf.format(new Date((new Date().getTime()-(i * 1000 * 60 * 60 * 24))));
            Map mm = new HashMap();
            mm.put("date", date);
            mm.put("value", temp.get(i));
            list.add(mm);
        }
        Collections.reverse(list);
        return RestResultGenerator.genSuccessResult(list);
    }
    @GetMapping("/newChannel")
    public Object tongJiNewChannel(){
        TreeMap map = (TreeMap) iMemberApi.tongJiNewChannel();
        List<Map<String, String>> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");

        List temp = new ArrayList(map.values());

        for (int i = 0; i < temp.size(); i++) {
            String date = sdf.format(new Date((new Date().getTime()-(i * 1000 * 60 * 60 * 24))));
            Map mm = new HashMap();
            mm.put("date", date);
            mm.put("value", temp.get(i));
            list.add(mm);
        }

        Collections.reverse(list);

        return RestResultGenerator.genSuccessResult(list);
    }



    @GetMapping("/payHis")
    public Object payHis(@RequestParam Integer curr,  String mId,  String nickName,String mobile){
        return RestResultGenerator.genSuccessResult(iMemberApi.payHisAll(curr, mId, nickName,mobile));
    }


    @PostMapping("/changeSpeakStatus")
    public Object changeSpeakStatus(@RequestParam String mId, @RequestParam Integer speakStatus){
        iMemberApi.changeStatus(mId, speakStatus);
        return RestResultGenerator.genSuccessResult("成功");
    }

    @PostMapping("/delete")
    public Object lookInfo(@RequestParam String mId) throws  Exception{
            iMemberApi.delete(mId);
        return RestResultGenerator.genSuccessResult("删除成功");
    }


    /**
     * =============================================后台api==============================================================
     */
    /**
     * 获取excel文件名
     * @param vo
     * @return
     */
    @GetMapping("/excelname")
    public Object getExcelName(MemberQueryVO vo){
        try {
            System.out.println("111111111111111111111111111111111111111111");
            return RestResultGenerator.genSuccessResult(iMemberApi.getAllExcl(vo));
        } catch (Exception e) {
            throw new RuntimeException("下载失败");
        }
    }

    /**
     * 下载Excel文件
     * @param request
     * @param response
     * @param filename
     */
    @GetMapping("/excelMemberId")

    public void getExcel(HttpServletRequest request, HttpServletResponse response, @RequestParam String filename){
        String fullPath = "D://upload//jiaai//excel//".concat(filename);
        File downloadFile = new File(fullPath);
        ServletContext context = request.getServletContext();

        String mimeType = context.getMimeType(fullPath);
        mimeType = "application/octet-stream; charset=utf-8";

        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = null;

        try {
            headerValue = "attachment; filename=".concat(new String("商品信息表.xlsx".getBytes(), "ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader(headerKey, headerValue);
        // Copy the stream to the response's output stream.
        try {
            InputStream myStream = new FileInputStream(fullPath);
            IOUtils.copy(myStream, response.getOutputStream());
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}

